package stardust.product.j2d.buffer;

import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;

public class GraphicsBuffer
{
	
	private int bufferSize;
    private int width;
    private int height;

    private GraphicsFrame[] buffer;
    
    /**
     * 构建图形缓冲区。
     * 
     * @param bufferSize	缓冲帧数量。
     * @param width			每个缓冲帧的宽度。
     * @param height		每个缓冲帧的高度。
     */
    public GraphicsBuffer(int bufferSize, int width, int height)
    {
        if (bufferSize < 2)
        {
        	throw new IllegalArgumentException("图形缓冲区至少需要2个缓冲帧空间。");
        }
        
        this.bufferSize = bufferSize;
        this.width = width;
        this.height = height;
    }

    /**
     * 从系统默认图形设备中申请资源构建绘图缓冲区。
     */
    public void initialize()
    {
    	this.buffer = new GraphicsFrame[this.bufferSize];
    	
        Image img;
        for (int i = 0; i < this.buffer.length; i++)
        {
            img = GraphicsEnvironment
            		.getLocalGraphicsEnvironment()
            		.getDefaultScreenDevice()
            		.getDefaultConfiguration()
            		.createCompatibleImage(this.width, this.height);
//        	img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
            this.buffer[i] = new GraphicsFrame( -1, img);
        }
    }
    
    public int getWidth()
    {
		return width;
	}

	public int getHeight()
	{
		return height;
	}

	private long imageIndexCount;
    private int imageIndex;

    public Image getImage()
    {
        if (this.imageIndexCount >= this.graphicsIndexCount)
        {
            return null;
        }
        else
        {
        	this.imageIndexCount++;
            Image currentImage = buffer[this.imageIndex].getImage();

            if (this.imageIndex + 1 < this.buffer.length)
            {
            	this.imageIndex++;
            }
            else
            {
            	this.imageIndex = 0;
            }
            
            return currentImage;
        }
    }

    private long graphicsIndexCount;
    private int graphicsIndex;

    public Graphics getPaintGraphics()
    {
        // 如果缓冲区尚未显示完,而绘图已经将其他缓冲区绘制完成了,并且没有缓冲区可以供绘制,则返回 null。
        if (this.graphicsIndexCount - this.imageIndexCount > this.bufferSize)
        {
            return null;
        }
        else
        {
        	this.graphicsIndexCount++;
            Image currentImage = this.buffer[graphicsIndex].getImage();

            if (this.graphicsIndex + 1 < this.buffer.length)
            {
            	this.graphicsIndex++;
            }
            else
            {
            	this.graphicsIndex = 0;
            }

            return currentImage.getGraphics();
        }
    }

    public long getImageIndexCount()
    {
        return this.imageIndexCount;
    }

    public long getGraphicsIndexCount()
    {
        return this.graphicsIndexCount;
    }

    public int getBufferSize()
    {
        return this.bufferSize;
    }
}
